home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / noweb / src / c / finduses.c < prev    next >
C/C++ Source or Header  |  1995-02-24  |  4KB  |  132 lines

  1. #line 4 "finduses.nw"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "errors.h"
  7. #include "match.h"
  8. #include "getline.h"
  9. #include "recognize.h"
  10. #line 18 "finduses.nw"
  11. static Recognizer nwindex;
  12. #define ALPHANUM "_'@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#"
  13. #define SYMBOLS "!%^&*-+:=|~<>./?`"
  14. /* note $ and \ both delimiters */
  15. #line 26 "finduses.nw"
  16. static int showquotes = 1;
  17. #line 90 "finduses.nw"
  18. typedef struct line_and_outfile {
  19.     char *line;
  20.     FILE *out;
  21. } LineOut;
  22. #line 61 "finduses.nw"
  23. static void read_ids(FILE *in);
  24. #line 95 "finduses.nw"
  25. static void add_use_markers(FILE *in, FILE *out);
  26. #line 148 "finduses.nw"
  27. static void write_index_use(void *closure, char *id, char *instance);
  28. static char *emit_up_to(FILE *f, char *s, char *limit);
  29. #line 30 "finduses.nw"
  30. main(int argc, char **argv) {
  31.     FILE *fp;
  32.     char *myself = *argv;
  33.     int i;
  34.  
  35.     for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != 0; i++)
  36.         if (!strcmp(argv[i], "-noquote"))
  37.             showquotes = 0;
  38.         else
  39.             errormsg(Error, "%s: unknown option -%c\n", myself, argv[i][1]);
  40.     nwindex = new_recognizer(ALPHANUM, SYMBOLS);
  41.     if (i == argc) {
  42.        
  43. #line 72 "finduses.nw"
  44. {   FILE *tmp = tmpfile();
  45.     char *line;
  46.     if (tmp == NULL) 
  47. #line 151 "finduses.nw"
  48. errormsg(Fatal, "%s: couldn't open temporary file\n");
  49. #line 75 "finduses.nw"
  50.     while ((line = getline(stdin)) != NULL) {
  51.         if (fputs(line, tmp) == EOF) 
  52. #line 153 "finduses.nw"
  53. errormsg(Fatal, "%s: error writing temporary file\n");
  54. #line 77 "finduses.nw"
  55.         if (is_index(line, "defn")) {
  56.             if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
  57.             add_ident(nwindex, line+1+5+1+4+1);
  58.         } else if (is_index(line, "localdefn")) {
  59.             if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
  60.             add_ident(nwindex, line+1+5+1+9+1);
  61.         }
  62.     }
  63.     rewind(tmp);
  64.     stop_adding(nwindex);
  65.     add_use_markers(tmp, stdout);
  66. }
  67. #line 43 "finduses.nw"
  68.     } else {
  69.        
  70. #line 53 "finduses.nw"
  71. for (; i < argc; i++)
  72.     if ((fp=fopen(argv[i],"r"))==NULL)
  73.         errormsg(Error, "%s: couldn't open file %s\n", myself, argv[i]);
  74.     else {
  75.         read_ids(fp);
  76.         fclose(fp);
  77.     }
  78. #line 45 "finduses.nw"
  79.        stop_adding(nwindex);
  80.        add_use_markers(stdin, stdout);
  81.     }
  82.     exit(errorlevel);
  83.     return errorlevel;          /* slay warning */
  84. }
  85. #line 63 "finduses.nw"
  86. static void read_ids(FILE *in) {
  87.     char *line;
  88.     while ((line = getline(in)) != NULL) {
  89.         if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
  90.         add_ident(nwindex, line);
  91.     }
  92. }
  93. #line 97 "finduses.nw"
  94. static void add_use_markers(FILE *in, FILE *out) {
  95.     char *line;
  96.     int incode = 0;
  97.     LineOut info; info.line = (char *)0; info.out = out;
  98.     
  99.     while ((line = getline(in)) != NULL) {
  100.         if (is_begin(line, "code") || showquotes && is_keyword(line, "quote"))
  101.             incode = 1;
  102.         else if (is_end(line, "code") || is_keyword(line, "endquote"))
  103.             incode = 0;
  104.         if (is_keyword(line, "text") && incode) {
  105.             info.line = line + 6; /* skip "@text " */
  106.             search_for_ident(nwindex, line, write_index_use, &info);
  107.             if (*info.line && *info.line != '\n') 
  108.                 fprintf(out, "@text %s", info.line);    /* has newline */
  109.         } else
  110.             fprintf(out, "%s", line);
  111.     }       
  112. }
  113. #line 128 "finduses.nw"
  114. static void write_index_use(void *closure, char *id, char *instance) {
  115.   LineOut *info = (LineOut *) closure;
  116.   info->line = emit_up_to(info->out, info->line, instance);
  117.   fprintf(info->out, "@index use %s\n", id);
  118.   info->line = emit_up_to(info->out, info->line, instance + strlen(id));
  119. }
  120. #line 136 "finduses.nw"
  121. static char *emit_up_to(FILE *f, char *s, char *limit) {
  122.   if (s < limit) {
  123.     char saved = *limit;
  124.     *limit = 0;
  125.     fprintf(f, "@text %s\n", s);
  126.     *limit = saved;
  127.     return limit;
  128.   } else {
  129.     return s;
  130.   }
  131. }
  132.